package com.casems.service.impl;

import com.casems.commons.dto.PermissionDTO;
import com.casems.entity.Permission;
import com.casems.mapper.PermissionMapper;
import com.casems.service.PermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tch
 * @since 2023-05-19
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Autowired
    PermissionMapper permissionMapper;

    @Override
    public void setChildren(PermissionDTO top, List<PermissionDTO> permissionList) {
        List<PermissionDTO> children = new ArrayList<>();
        for (Permission permission : permissionList) {
            if (permission.getPid() == top.getId()){
                children.add((PermissionDTO) permission);
            }
        }
        top.setChildren(children);
        if (children.size() > 0) {
            children.forEach(child->setChildren(child, permissionList));

        }
    }

    @Override
    public List<PermissionDTO> getAllPermission() {
        return permissionMapper.selectPermissionAll();
    }
}
